﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using System.Diagnostics;
using SharpDiary.Core.Data;
using SharpDiary.Core.Utility;
using SharpDiary.Core.ORMapping;
using SharpDiary.Core.Drawing;
using SharpDiary.Shell.Properties;

namespace Neotoma.Shell
{
    public partial class frmSplash : Form
    {
        private string _imageFileName = string.Empty;
        private bool _mouseEntered = false;
        private bool _autoScaleBackgroundImageSize = true;  //是否自动调整背景图显示大小。

        /// <summary>
        /// 获取或设置背景图片。
        /// </summary>
        public string ImageFileName
        {
            get { return _imageFileName; }
            set
            {
                if (value != null && System.IO.File.Exists(value))
                {
                    _imageFileName = value;

                    this.BackgroundImage = Image.FromFile(value);
                }
            }
        }

        /// <summary>
        /// 停留时间(毫秒)。
        /// </summary>
        public int DelayTime
        {
            get { return timerDelay.Interval; }
            set { timerDelay.Interval = value; }
        }

        #region Constructors

        public frmSplash()
        {
            InitializeComponent();

            DoubleBuffered = true;

            LoadThenUseSettings();
            LoadBackgroundImage();
        }

        #endregion

        /// <summary>
        /// 加载背景图片。
        /// </summary>
        private void LoadBackgroundImage()
        {
            _imageFileName = SharpDiary.Shell.Properties.Settings.Default.SplashImageFileName;
            if (System.IO.File.Exists(_imageFileName))
            {
                this.BackgroundImage = Image.FromFile(_imageFileName);

                if (!_autoScaleBackgroundImageSize)
                {
                    this.BackgroundImageLayout = ImageLayout.Center;
                    if (this.BackgroundImage != null)
                    {
                        this.Size = this.BackgroundImage.Size;
                    }
                }
            }
        }

        /// <summary>
        /// 加载并应用配置信息。
        /// </summary>
        private void LoadThenUseSettings()
        {
            lblVersion.ForeColor = Settings.Default.VersionLabelForeColor;
            lblTotalCountLeft.ForeColor = Settings.Default.DiaryCountLeftLabelForeColor;
            lblTotalDiaryCount.ForeColor = Settings.Default.DiaryCountLabelForeColor;
            _autoScaleBackgroundImageSize = Settings.Default.AutoScaleBackgroundImageSize;
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);

            DisplayInfo();
            timerDelay.Start();
        }

        private void UpdateRegion()
        {
            this.Region = new Region(PaintHelper.GetRoundedRect(this.ClientRectangle, 3f, 3f, 6f, 6f));
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
        }

        /// <summary>
        /// 显示相关信息。
        /// </summary>
        private void DisplayInfo()
        {
            try
            {
                lblVersion.Text = string.Format("版本: {0}", Utils.GetFileVersion());
                Application.DoEvents();

                lblTotalDiaryCount.Text = DiaryInfo.GetTotalDiaryCount().ToString();
                Application.DoEvents();
            }
            finally
            {
                lblTotalCountLeft.Location = new Point((this.Width - lblTotalCountLeft.Width - lblTotalDiaryCount.Width) / 2,
                    (int)(this.Height * 0.618));
                lblTotalDiaryCount.Location = new Point( lblTotalCountLeft.Left + lblTotalCountLeft.Width,
                    lblTotalCountLeft.Top - 2);

                lblTotalCountLeft.Show();
                lblTotalDiaryCount.Show();
            }
        }

        // Define the CS_DROPSHADOW constant
        private const int CS_DROPSHADOW = 0x00020000;

        //使窗体显示阴影
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ClassStyle |= CS_DROPSHADOW;
                return cp;
            }
        }

        private void frmSplash_FormClosed(object sender, FormClosedEventArgs e)
        {

        }

        private void timerDelay_Tick(object sender, EventArgs e)
        {
            timerTransparency.Stop();

            //如果光标正停留在 Splash 窗体上且Ctrl 键被按下，则不执行以下操作
            if (Control.ModifierKeys == Keys.Control && this.ClientRectangle.Contains(this.PointToClient(Cursor.Position)))
            {
                _mouseEntered = true;
                return;
            }

            timerDelay.Stop();
            Close();
        }

        private void frmSplash_MouseLeave(object sender, EventArgs e)
        {
            //如果曾经有意阻止过 Splash 窗体关闭，则在光标离开 Splash 窗体后，立即关闭。
            if (_mouseEntered)
            {
                Close();
            }
        }

        private void timerTransparency_Tick(object sender, EventArgs e)
        {
            if (this.Opacity + 0.1d < 0.9d)
            {
                this.Opacity += 0.1d;
                if (this.Opacity >= 1d) { timerTransparency.Stop(); }
            }
            else
            {
                timerTransparency.Stop();
            }
        }

        private void frmSplash_Shown(object sender, EventArgs e)
        {
            timerTransparency.Start();
        }
    }
}
